﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MarginTrading.Entity;
using MarginTrading.Common;
using MarginTrading.Services;
using System.Security.Cryptography;

namespace MarginTrading.Forms
{
    public partial class AddUser : Form
    {
        public AddUser()
        {
            InitializeComponent();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (validateInputs() == false)
            {
                return;
            }

            UserAccount account = new UserAccount();
            account.UserName = textBoxUserName.Text;
            account.Password = MD5Calculator.CalculateMD5Hash(textBoxPassword.Text);
            if (comboBoxUserType.Text == "Administrator")
            {
                account.UserType = 1;
            }
            else if (comboBoxUserType.Text == "Normal User")
            {
                account.UserType = 2;
            }
            else if (comboBoxUserType.Text == "Advisor")
            {
                account.UserType = 3;
            }
            else if (comboBoxUserType.Text == "Client")
            {
                try
                {
                    if (ClientService.GetClientByCode(textBoxUserName.Text) == null)
                    {
                        MessageBox.Show("Client code is invalid !", "Error");
                        return;
                    }
                }
                catch(Exception ee)
                {
                }

                account.UserType = 4;
            }
            else
            {
                MessageBox.Show("Invalid user type " + comboBoxUserType.Text, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            try
            {
                UserAccountServices.Add(account);
            }
            catch (System.Exception ex)
            {
                if (ex.Message.StartsWith("ORA-00001: unique"))
                {
                    MessageBox.Show("User " + textBoxUserName.Text + " already exists", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    MessageBox.Show("Failed to create new account\nError: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                return;
            }

            MessageBox.Show("User added successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //Close();
        }

        private bool validateInputs()
        {
            if (textBoxPassword.Text == "" || textBoxUserName.Text == "" || textBoxConfirmPass.Text == "")
            {
                MessageBox.Show("Please enter required details", "Required Fields not Filled", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return false;
            }

            if (textBoxConfirmPass.Text != textBoxPassword.Text)
            {
                MessageBox.Show("Confirmation password does not match the password", "Confirm Password Wrong", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return false;
            }

            return true;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void AddUser_Load(object sender, EventArgs e)
        {
            comboBoxUserType.SelectedIndex = 0;

        }

        private void comboBoxUserType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBoxUserType.Text == "Client")
            {
                try
                {
                    Config AgentCodeConfig = ConfigServices.GetConfigByName("Agent Code");
                    String AgentCode = AgentCodeConfig.Value;

                    AutoCompleteStringCollection data = new AutoCompleteStringCollection();
                    ClientCollection clientCollection = ClientService.GetClientCollectionEX(AgentCode);

                    for (int iI = 0; iI < clientCollection.Count; iI++)
                    {
                        data.Add(clientCollection[iI].Code);
                    }

                    textBoxUserName.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                    textBoxUserName.AutoCompleteSource = AutoCompleteSource.CustomSource;
                    textBoxUserName.AutoCompleteCustomSource = data;
                }
                catch (System.Exception ex)
                {

                }
            }
            else
            {
                textBoxUserName.AutoCompleteMode = AutoCompleteMode.None;
            }
        }

        private void textBoxUserName_TextChanged(object sender, EventArgs e)
        {
            textBoxPassword.Clear();
            textBoxConfirmPass.Clear();
        }



    }
}
